package com.lgmshare.component.helper;

import android.content.res.Resources;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.AbsoluteSizeSpan;
import android.text.style.BackgroundColorSpan;
import android.text.style.ForegroundColorSpan;
import android.text.style.RelativeSizeSpan;
import android.text.style.StrikethroughSpan;
import android.text.style.SubscriptSpan;
import android.text.style.SuperscriptSpan;
import android.text.style.UnderlineSpan;


/**
 * @author lim
 * @Description: 设置字体样式
 * @mail lgmshare@gmail.com
 * @date 2014年6月9日  上午10:37:00
 */
public class TextStyleHelper {

    private SpannableString mSpannableText;


    /**
     * Entry point into this API; pattern must be non-null.
     *
     * @throws IllegalArgumentException if pattern contains any syntax errors.
     */
    public static TextStyleHelper from(CharSequence pattern) {
        return new TextStyleHelper(pattern);
    }

    /**
     * Entry point into this API.
     *
     * @throws IllegalArgumentException if pattern contains any syntax errors.
     */
    public static TextStyleHelper from(Resources r, int patternResourceId) {
        return from(r.getText(patternResourceId));
    }

    private TextStyleHelper(CharSequence pattern) {
        mSpannableText = new SpannableString(pattern);
    }

    /**
     * @param size  相对文字大小
     * @param start
     * @param end
     * @return
     */
    public TextStyleHelper setRelativeSize(float size, int start, int end) {
        // 0.5f表示默认字体大小的一半
        mSpannableText.setSpan(new RelativeSizeSpan(size), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        return this;
    }

    /**
     * @param size  绝对文字大小
     * @param start
     * @param end
     * @param dp    文字大小单位是否用dp
     * @return
     */
    public TextStyleHelper setAbsoluteSize(int size, int start, int end, boolean dp) {
        // 第二个参数boolean dip，如果为true，表示前面的字体大小单位为dip，否则为像素，同上。
        mSpannableText.setSpan(new AbsoluteSizeSpan(size, dp), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        return this;
    }

    /**
     * 设置文字颜色
     *
     * @param color
     * @param start
     * @param end
     * @return
     */
    public TextStyleHelper setForegroundColor(int color, int start, int end) {
        // 设置前景色为洋红色
        mSpannableText.setSpan(new ForegroundColorSpan(color), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        return this;
    }

    /**
     * 文字背景色
     *
     * @param color
     * @param start
     * @param end
     * @return
     */
    public TextStyleHelper setBackgroundColor(int color, int start, int end) {
        // 设置前景色为洋红色
        mSpannableText.setSpan(new BackgroundColorSpan(color), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        return this;
    }

    /**
     * 设置下划线
     *
     * @param start
     * @param end
     * @return
     */
    public TextStyleHelper setUnderlineSpan(int start, int end) {
        mSpannableText.setSpan(new UnderlineSpan(), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        return this;
    }

    /**
     * 设置删除线
     *
     * @param start
     * @param end
     * @return
     */
    public TextStyleHelper setStrikethroughSpan(int start, int end) {
        mSpannableText.setSpan(new StrikethroughSpan(), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        return this;
    }

    /**
     * 文字上突
     *
     * @param start
     * @param end
     * @return
     */
    public TextStyleHelper setSubscriptSpan(int start, int end) {
        mSpannableText.setSpan(new SubscriptSpan(), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        return this;
    }

    /**
     * 文字下突
     *
     * @param start
     * @param end
     * @return
     */
    public TextStyleHelper setSuperscriptSpan(int start, int end) {
        mSpannableText.setSpan(new SuperscriptSpan(), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        return this;
    }

    public CharSequence format() {
        return mSpannableText;
    }
}